<?php

declare(strict_types=1);

namespace AdminBundle\Http\Controllers\Admin\V1;

use App\Controller\AbstractController;
use Hyperf\Di\Annotation\Inject;
use AdminBundle\Services\MenuService;
use AdminBundle\Services\PermissionsService;

/**
 * MenuController
 * 菜单控制器
 * 
 */
class MenuController extends AbstractController
{
    /**
     * @Inject
     * @var MenuService
     */
    public $service;

    /**
     * @Inject
     * @var PermissionsService
     */
    public $permissionsService;

    /**
     * index
     * 函数的含义说明
     * @return \Psr\Http\Message\ResponseInterface
     * 
     */
    public function index()
    {
        $list = $this->service->getMenuTreeList();

        $data = [
            'list' => $list
        ];

        return $this->response->success($data);
    }

    /**
     * getroutes
     * 获取用户权限对应的菜单
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function getroutes()
    {
        $userinfo = $this->request->getAttribute('auth');
        $list = $this->service->getUserMenuList($userinfo->id);
        $data = [
            'list' => $list
        ];

        return $this->response->success($data);
    }

    /**
     * getPermissions
     * 获取权限列表，创建菜单需要配置权限
     * @return \Psr\Http\Message\ResponseInterface
     *
     */
    public function getPermissions()
    {
        $list = $this->permissionsService->getPermissionsTreeList();

        $data = [
            'list' => $list
        ];

        return $this->response->success($data);
    }

    /**
     * store
     * 菜单保存，新建、编辑都用该方法，区别是否有主键id
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function store()
    {
        $reqParam = $this->request->all();

        if ( !(isset($reqParam['id']) && $reqParam['id']) ) {
            $reqParam['sort'] = $this->service->getMenuCount(['parent_id' => $reqParam['parent_id']]);
        }

        $id = $this->service->saveMenu($reqParam);

        return $this->response->success($id);
    }

    /**
     * getInfo
     * 根据id获取单条记录信息
     * @return \Psr\Http\Message\ResponseInterface
     *
     */
    public function getInfo($id)
    {
        $info = $this->service->getInfo($id);
        $pid = isset($info['system_permission_id']) ? $info['system_permission_id'] : 0;
        $menuPermissions = $this->service->getMenuPermissionList($pid);

        $data = [
            'info' => $info,
            'menu_permissions' => $menuPermissions,
        ];

        return $this->response->success($data);
    }

    /**
     * orderMenu
     * 菜单的拖拽排序
     * @return \Psr\Http\Message\ResponseInterface
     *
     */
    public function orderMenu()
    {
        $reqParam = $this->request->all();

        $this->service->orderMenu($reqParam['ids']);

        return $this->response->success('ok');
    }

    /**
     * destroy
     * 删除菜单
     * @return \Psr\Http\Message\ResponseInterface
     *
     */
    public function destroy($id)
    {
        $this->service->deleteInfo($id);

        return $this->response->success('ok');
    }
}